package sprites
{
	import UIComponent.PointSprite;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayList;
	import mx.controls.Button;
	import mx.events.PropertyChangeEvent;
	
	import skeletons.CaseSkeleton;
	import skeletons.DataSourceSkeleton;
	
	public class CaseSprite extends PointSprite
	{
		//component
		private var addDataSource:Button;
		
		//static
		private var ADDDATASOURCE_WIDTH:Number=25;
		
		public function CaseSprite(_caseSkeleton:CaseSkeleton, _canvus:MyCanvas)
		{
			//DEFAULT_MINI_WIDTH=280;
			super(_caseSkeleton,_canvus);
			this.setColor(CASE_COLOR);
			
			//addDataSource button
			addDataSource= new Button();
			this.addChild(addDataSource);
			addDataSource.label= "+";
			addDataSource.width= ADDDATASOURCE_WIDTH;
			addDataSource.height= DEFAULT_MINI_HEIGHT - MARGIN_HEIGHT*2;
			addDataSource.x= DEFAULT_MINI_WIDTH - MARGIN_WIDTH*3 - ADDDATASOURCE_WIDTH - CONVERT_WIDTH - REMOVE_WIDTH;
			addDataSource.y= MARGIN_HEIGHT;
			addDataSource.addEventListener(MouseEvent.CLICK, onAddDataSource);
		}
		
		override protected function onSelected(evt:MouseEvent):void
		{
			if(canvas.getSelectedCase()==this)
			{
				canvas.setSelectedCase(null);
				this.selected=false;
				this.invalidateDisplayList();
			}
			else
			{
				if(canvas.getSelectedCase()==null)
				{
					
				}
				else
				{
					canvas.getSelectedCase().selected=false;
					canvas.getSelectedCase().invalidateDisplayList();
				}
				canvas.setSelectedCase(this);
				this.selected=true;
				this.invalidateDisplayList();
			}
		}
		
		override public function remove():void
		{
			canvas.removeChild(this);
		}
		
		override protected function onRemove(evt:MouseEvent):void
		{
			canvas.curProject.removeCase((CaseSkeleton)(this.getSkeleton()));
		}
		
		protected function onAddDataSource(evt:MouseEvent):void
		{
			canvas.curProject.sendAddData((CaseSkeleton)(this.getSkeleton()).getCID());
			
//			var _parent:CaseSkeleton = (CaseSkeleton)(this.getSkeleton());
//			var tempData:DataSourceSkeleton=new DataSourceSkeleton(_parent);
//			tempData.setName("Data Source");
//			tempData.setDiscription("Discription");
//			if(_parent.getDataSourceList()==null)
//			{
//				_parent.setDataSourceList(new ArrayList());
//			}
//			_parent.getDataSourceList().addItem(tempData);
//			canvas.invalidateDisplayList();
		}

		override protected function onChanged(evt:PropertyChangeEvent):void
		{
			canvas.curProject.modifyName(this.getSkeleton(),(String)(evt.newValue),0);
		}
		
		override protected function onDiscriptionChanged(evt:PropertyChangeEvent):void
		{
			canvas.curProject.modifyDiscription(this.getSkeleton(),(String)(evt.newValue),0);
		}
		
	}
}